package cn.jeasy.core.context;

import cn.jeasy.core.bean.factory.DefaultBeanFactory;

import java.util.Map;

import cn.jeasy.core.bean.BeanDefinition;
import cn.jeasy.core.bean.DefaultBeanDefinitionReader;

/**
 * 通过自动扫描包的方式实现容器bean
 * @author Fred
 *
 */
public class AutowiredApplicationContext extends AbstractApplicationContext {

	private String basePackage;
	
	public AutowiredApplicationContext(String basePackage) throws Exception {
		this(basePackage,  DefaultBeanFactory.getInstance());
		
	}
	
	public AutowiredApplicationContext(String basePackage,DefaultBeanFactory beanFactory) throws Exception {
		super(beanFactory);
		this.basePackage=basePackage;
		refresh();
	}

	@Override
	protected void loadBeanDefinitions(DefaultBeanFactory beanFactory) throws Exception {
	   DefaultBeanDefinitionReader reader=new DefaultBeanDefinitionReader();
	   reader.loadBeanDefinitionsByPackage(basePackage);
	   // 将加载出的bean定义从registry中注册到beanFactory中
		for (Map.Entry<String, BeanDefinition> beanDefinitionEntry : reader.getRegistry().entrySet()) {
				beanFactory.registerBeanDefinition(beanDefinitionEntry.getKey(), beanDefinitionEntry.getValue());
		}
	}


	
}
